package com.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.messaging.core.AbstractMessageSendingTemplate;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.config.AbstractMessageBrokerConfiguration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.messaging.simp.config.SimpleBrokerRegistration;
import org.springframework.messaging.simp.config.StompBrokerRelayRegistration;
import org.springframework.messaging.simp.user.SimpSubscription;
import org.springframework.messaging.simp.user.SimpSubscriptionMatcher;
import org.springframework.messaging.simp.user.SimpUser;
import org.springframework.messaging.simp.user.SimpUserRegistry;
import org.springframework.util.ObjectUtils;

import java.util.Set;

@Configuration
public class MqConfig extends AbstractMessageBrokerConfiguration {


    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableStompBrokerRelay("/topic","/queue");
    }
    private static class TestUserRegistry implements SimpUserRegistry, Ordered {

        private Integer order;

        public void setOrder(int order) {
            this.order = order;
        }

        @Override
        public int getOrder() {
            return this.order;
        }

        @Override
        public SimpUser getUser(String userName) { return null; }

        @Override
        public Set<SimpUser> getUsers() { return null; }

        @Override
        public int getUserCount() { return 0; }

        @Override
        public Set<SimpSubscription> findSubscriptions(SimpSubscriptionMatcher matcher) { return null; }
    }

    @Override
    protected SimpUserRegistry createLocalUserRegistry(Integer order) {
        TestUserRegistry testUserRegistry = new TestUserRegistry();
        if(!ObjectUtils.isEmpty(order)){
            testUserRegistry.setOrder(order);
        }
        return testUserRegistry;
    }
}
